package com.sean.bigdata.chapter03;

public class ArrayStack<T> implements StackADT {

    Object[] elements;
    private static final int DEFAULTSIZE = 10;
    private int size = -1;
    private int maxLength;

    public ArrayStack() {
        elements = new Object[DEFAULTSIZE];
        maxLength = 10;
    }

    public ArrayStack(int size) {
        elements = new Object[size];
        maxLength = size;
    }

    @Override
    public boolean push(Object element) throws StackOverMaxLength {
        if (size == maxLength) {
            throw new StackOverMaxLength("超过最大值");
        }
        size++;
        elements[size] = element;
        return true;

    }

    @Override
    public Object pop() throws StackEmptyException {
        if (size == -1) {
            throw new StackEmptyException("空的栈");
        }

        Object data = elements[size];
        size--;
        return data;
    }

    @Override
    public int size() {
        return size + 1;
    }

    @Override
    public boolean isEmpty() {
        return size == -1 ? true : false;
    }

    @Override
    public Object peek() {
        if (size != -1)
            return elements[size];
        return null;
    }
}
